package com.largeOrange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.largeOrange.mapper.CategoryMapper;
import com.largeOrange.myexception.ConnectionCategoryException;
import com.largeOrange.pojo.Category;
import com.largeOrange.pojo.Dish;
import com.largeOrange.pojo.Setmeal;
import com.largeOrange.service.CategoryService;
import com.largeOrange.service.DishService;
import com.largeOrange.service.SetMealService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Map;

/**
 * @author 大橙子
 * @version 1.0
 * @since 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    SetMealService setMealService;
    @Autowired
    DishService dishService;

    /**
     * 重写业务方法
     *
     * @param id
     * @return
     */
    @SneakyThrows
    @Override
    public boolean removeById(Serializable id) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        //查询
        Map<String, Object> setMealCount = setMealService.getMap(setmealLambdaQueryWrapper);
        Map<String, Object> dishCount = dishService.getMap(dishLambdaQueryWrapper);
        if (setMealCount != null || dishCount != null) {
            throw new ConnectionCategoryException("已关联其他菜品，不能删除！");
        }
        return super.removeById(id);
    }
}
